package com.qckj.amz.domain.spapi.feeds.home.ProductFeedXml;

import com.qckj.amz.domain.spapi.feeds.home.AmazonEnvelope;
import com.qckj.amz.domain.spapi.feeds.home.Header;
import com.qckj.amz.domain.spapi.feeds.home.Message;
import com.qckj.amz.utils.constants.AmzConstants;
import org.apache.commons.lang3.StringUtils;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;


/*
*
* 上传产品xml示例，最终java对象封装结果

<?xml version="1.0" encoding="UTF-8"?>
<AmazonEnvelope
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
    <Header>
        <DocumentVersion>1.01</DocumentVersion>
        <MerchantIdentifier>ARBGXXE1RPNJC</MerchantIdentifier>
    </Header>
    <MessageType>Product</MessageType>
    <PurgeAndReplace>false</PurgeAndReplace>
    <Message>
        <MessageID>1</MessageID>
        <OperationType>Update</OperationType>
        <Product>
            <SKU>sdsd</SKU>
            <StandardProductID>
                <Type>EAN</Type>
                <Value>sss</Value>
            </StandardProductID>
            <Condition>
                <ConditionType>New</ConditionType>
            </Condition>
            <DescriptionData>
                <Title>Example Product Title</Title>
				<Brand>Example Product Brand</Brand>
                <Description>This is an example product description.</Description>
				<BulletPoint>Example Bullet Point 1</BulletPoint>
				<BulletPoint>Example Bullet Point 2</BulletPoint>
				<BulletPoint>Example Bullet Point 3</BulletPoint>
				<BulletPoint>Example Bullet Point 4</BulletPoint>
				<BulletPoint>Example Bullet Point 5</BulletPoint>
                <Manufacturer>sssss</Manufacturer>
                <MfrPartNumber>sdsd</MfrPartNumber>
                <SearchTerms>1</SearchTerms>
                <RecommendedBrowseNode>1231358545</RecommendedBrowseNode>
                <CountryOfOrigin>cn</CountryOfOrigin>
            </DescriptionData>
            <ProductData>
                <Home>
                    <ProductType>
                        <Home>
                            <VariationData>
                                <VariationTheme>Size-Color</VariationTheme>
                            </VariationData>
                        </Home>
                    </ProductType>
                    <Parentage>parent</Parentage>
                    <Material>suliao</Material>
                </Home>
            </ProductData>
        </Product>
    </Message>
    <Message>
        <MessageID>2</MessageID>
        <OperationType>Update</OperationType>
        <Product>
            <SKU>sku-XXL-blue</SKU>
            <StandardProductID>
                <Type>EAN</Type>
                <Value>sssdsssds</Value>
            </StandardProductID>
            <Condition>
                <ConditionType>New</ConditionType>
            </Condition>
            <DescriptionData>
                <Title>1</Title>
                <Brand>brand</Brand>
                <Description>1</Description>
				<BulletPoint>Example Bullet Point 1</BulletPoint>
				<BulletPoint>Example Bullet Point 2</BulletPoint>
				<BulletPoint>Example Bullet Point 3</BulletPoint>
				<BulletPoint>Example Bullet Point 4</BulletPoint>
				<BulletPoint>Example Bullet Point 5</BulletPoint>
                <MaxOrderQuantity>10</MaxOrderQuantity>
                <Manufacturer>sssss</Manufacturer>
                <MfrPartNumber>sdsd</MfrPartNumber>
                <SearchTerms>1</SearchTerms>
                <RecommendedBrowseNode>1231358545</RecommendedBrowseNode>
                <CountryOfOrigin>cn</CountryOfOrigin>
            </DescriptionData>
            <ProductData>
                <Home>
                    <ProductType>
                        <Home>
                            <VariationData>
                                <VariationTheme>Size-Color</VariationTheme>
                                <Size>XXL</Size>
                                <Color>blue</Color>
                            </VariationData>
                        </Home>
                    </ProductType>
                    <Parentage>child</Parentage>
                    <Material>suliao</Material>
                </Home>
            </ProductData>
        </Product>
    </Message>
</AmazonEnvelope>
*
* */

/**
 * Home类目，Java对象转xml字符串
 */
public class TestCreateXml {
    public static void main(String[] args) throws JAXBException {


        /**
         * 八级/VariationData
         * 当二级Message为父体时，只赋值VariationTheme，若为子体时，需要setSize()和setColor()
         */
        VariationData variationData = new VariationData();
        variationData.setVariationTheme("Size-Color");

        /**
         * 七级/Home
         */
        Home c_home = new Home();
        c_home.setVariationData(variationData);

        /**
         * 六级/ProductType
         */
        ProductType productType = new ProductType();
        productType.setHome(c_home);

        /**
         * 五级/Home
         */
        Home p_home = new Home();
        p_home.setProductType(productType);
        p_home.setParentage(AmzConstants.PARENTAGE_PARENT);//父体
        //p_home.setMaterial("suliao");//材质

        //封装五点描述/BulletPoint
        List<String> bulletPointList = new ArrayList<>();
        bulletPointList.add("Example Bullet Point 1");
        bulletPointList.add("Example Bullet Point 2");
        bulletPointList.add("Example Bullet Point 3");
        bulletPointList.add("Example Bullet Point 4");
        bulletPointList.add("Example Bullet Point 5");

        /**
         * 四级/ProductData
         */
        ProductData productData = new ProductData();
        productData.setHome(p_home);

        /**
         * 四级/StandardProductID
         */
        StandardProductID standardProductID = new StandardProductID();
        standardProductID.setType("EAN");
        standardProductID.setValue("EAN123456");

        /**
         * 四级/Condition
         */
        Condition condition = new Condition();
        condition.setConditionType("New");

        /**
         * 四级/DescriptionData
         */
        DescriptionData descriptionData = new DescriptionData();
        descriptionData.setTitle("Example Product Title");
        descriptionData.setBrand("Brand QIAOCHUANKEJI");
        descriptionData.setDescription("This is an example product description.");
        descriptionData.setManufacturer("Manufacturer QIAOCHUANKEJI");
        descriptionData.setMfrPartNumber("MfrPartNumber 12345");
        descriptionData.setSearchTerms("keyword");
        descriptionData.setRecommendedBrowseNode("12345678");
        descriptionData.setCountryOfOrigin("CN");
        descriptionData.setBulletPoint(bulletPointList);

        /**
         * 三级/Product
         */
        Product product = new Product();
        product.setSKU("sku123456");
        product.setStandardProductID(standardProductID);
        product.setCondition(condition);
        product.setDescriptionData(descriptionData);
        product.setProductData(productData);

        /**
         * 二级/Message,此节点为List对象，可以包含主体、变体
         */
        List<Message> messageList = new ArrayList<>();
        Message message = new Message();
        message.setMessageID(1L);
        message.setOperationType("Update");
        message.setProduct(product);
        messageList.add(message);

        /**
         * 二级/Header
         */
        Header header = new Header();
        header.setDocumentVersion("1.01");
        header.setMerchantIdentifier("ARBGXXE1RPNJC");

        /**
         * 一级/根节点AmazonEnvelope
         */
        AmazonEnvelope ae = new AmazonEnvelope();
        ae.setHeader(header);
        ae.setMessageType("Product");
        ae.setPurgeAndReplace("false");
        ae.setMessage(messageList);


        /**
         * 实现java对象转xml字符串
         */
        JAXBContext jaxbContext = JAXBContext.newInstance(AmazonEnvelope.class);
        StringWriter writer = new StringWriter();
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.marshal(ae, writer);
        String xmlStr = writer.toString();
        xmlStr = StringUtils.replace(xmlStr, "&quot;", "'");

        System.out.println(xmlStr);


    }
}
